﻿package org.exodia.seeds.ppv {
	import org.exodia.seeds.model.World;
	import org.papervision3d.lights.PointLight3D;
	import org.papervision3d.materials.ColorMaterial;
	import org.papervision3d.materials.shadematerials.GouraudMaterial;
	import org.papervision3d.materials.shaders.GouraudShader;
	import org.papervision3d.materials.shaders.ShadedMaterial;
	import org.papervision3d.materials.utils.MaterialsList;
	import org.papervision3d.materials.WireframeMaterial;
	import org.papervision3d.objects.DisplayObject3D;
	import org.papervision3d.objects.primitives.Cube;
	/**
	* ...
	* @author Default
	*/
	public class WorldPPV extends DisplayObject3D {
		private var _world:World;
		private var _arrAgentsPPV:Array;
		private var _bounds:Cube;
		
		public function WorldPPV( world:World ) {
			_world = world;
			_arrAgentsPPV = [];
			
			
			var wire:WireframeMaterial = new WireframeMaterial();
			var solid:ColorMaterial = new ColorMaterial( 0x0000B3 );
			solid.doubleSided = true;
			var light:PointLight3D = new PointLight3D();
			light.moveUp( 100 );
			light.moveRight( 300 );
			var mat:GouraudMaterial = new GouraudMaterial( light, 0xFFFFFF, 0x0000B3 );
			mat.doubleSided = true;
			var materialList:MaterialsList = new MaterialsList( {
				all: mat,
				back: wire
			} );
			_bounds = new Cube( materialList, 1, 1, 1 );
			addChild( _bounds );
		}

		public function addAgent( agentPPV:AgentPPV ) {
			_arrAgentsPPV.push( agentPPV );
			this.addChild( agentPPV );
			
			_bounds.scaleX = _world.width;
			_bounds.scaleY = _world.height;
			_bounds.scaleZ = _world.depth;
			//_bounds.x = _world.width/2;
			//_bounds.y = _world.height/2;
			//_bounds.z = _world.depth/2;
		}
		
		public function tick( deltaMS:uint ) : void {
			_world.tick( deltaMS );
			for each( var agentPPV:AgentPPV in _arrAgentsPPV ) {
				agentPPV.x = agentPPV.agent.position.x;
				agentPPV.y = agentPPV.agent.position.y;
				agentPPV.z = agentPPV.agent.position.z;
				agentPPV.rotationZ = agentPPV.agent.speed.angle2D;
			}
		}
	}
	
}